﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
		<title>确认下载范围</title>
		<style type="text/css">
			html, body {
				height: 100%;
				overflow: hidden;
			}
			body {
				margin: 0;
			}
			#map {
				height: 100%;
			}
		</style>
		<script src="http://ditu.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAxeA5H8HrULf2I9CaEu7XaBTD2YhpMV9QW0FbOwASuUzpDr5UrRRFthBtZv_2UbU9lISKBVIaxsby2w" type="text/javascript"></script>
		<script>
		//<![CDATA[

		var map, placemarks;
		var LatLonBox = {
        "north": 53.5608560,
        "south": 18.1612640,
        "east": 134.7739260,
        "west": 73.5577010
		};

		var adjustDownloadBox = function() {
			map.clearOverlays();
			var p = new GPolyline([
				new GLatLng(LatLonBox.north,LatLonBox.west),
				new GLatLng(LatLonBox.south,LatLonBox.west),
				new GLatLng(LatLonBox.south,LatLonBox.east),
				new GLatLng(LatLonBox.north,LatLonBox.east),
				new GLatLng(LatLonBox.north,LatLonBox.west)
			], '#f00', 3, 1.0);

			var bounds = p.getBounds();
			var center = bounds.getCenter();
			var zoomLevel = map.getBoundsZoomLevel(bounds);
			map.setCenter(center, zoomLevel);

			map.addOverlay(p);
			GEvent.addListener(p, "click", function(latlng) {
				map.openInfoWindowHtml(center, '中心点：' + center.toUrlValue() + '<br>'
					+ '宽度：' + (new GLatLng(LatLonBox.south,LatLonBox.west).distanceFrom(new GLatLng(LatLonBox.south,LatLonBox.east)) / 1000).toFixed(2) + '<br>'
					+ '高度：' + (new GLatLng(LatLonBox.north,LatLonBox.west).distanceFrom(new GLatLng(LatLonBox.south,LatLonBox.west)) / 1000).toFixed(2)
				);
			});

			var northMarker = new GMarker(new GLatLng(LatLonBox.north, (LatLonBox.east + LatLonBox.west) / 2), {draggable: true});
			map.addOverlay(northMarker);
			GEvent.addListener(northMarker, "dragend", function() {
				LatLonBox.north = northMarker.getLatLng().lat();
				adjustDownloadBox();
			});

			var eastMarker = new GMarker(new GLatLng((LatLonBox.north + LatLonBox.south) / 2, LatLonBox.east), {draggable: true});
			map.addOverlay(eastMarker);
			GEvent.addListener(eastMarker, "dragend", function() {
				LatLonBox.east = eastMarker.getLatLng().lng();
				adjustDownloadBox();
			});

			var southMarker = new GMarker(new GLatLng(LatLonBox.south, (LatLonBox.east + LatLonBox.west) / 2), {draggable: true});
			map.addOverlay(southMarker);
			GEvent.addListener(southMarker, "dragend", function() {
				LatLonBox.south = southMarker.getLatLng().lat();
				adjustDownloadBox();
			});

  		var westMarker = new GMarker(new GLatLng((LatLonBox.north + LatLonBox.south) / 2, LatLonBox.west), {draggable: true});
  		map.addOverlay(westMarker);
			GEvent.addListener(westMarker, "dragend", function() {
				LatLonBox.west = westMarker.getLatLng().lng();
				adjustDownloadBox();
			});
		}

		var downloadIt = function(index) {
				LatLonBox = placemarks[index].ExtendedData.LatLonBox;
				adjustDownloadBox();
				placemarks = null;
		}

		function setupMap() {
			if (GBrowserIsCompatible()) {
				map = new GMap2(document.getElementById("map"));
				map.setUIToDefault();
				
				adjustDownloadBox();

				var geocoder = new GClientGeocoder();
				var marker;

				GEvent.addListener(map, "click", function(overlay, latlng) {
					if (latlng != null) {
						geocoder.getLocations(latlng, function(response) {
							if (marker) {
								map.removeOverlay(marker);
							}

							if (!response || response.Status.code != 200) {
								alert("嗯，你点击的这个地方还没有准确地址！"+"状态码（Status Code）:" + response.Status.code);
							} else {
								placemarks = response.Placemark;
								var s = [];
								s.push('<select onchange="downloadIt(this.selectedIndex);">');
								for (var i = 0; i < placemarks.length; i++) {
									s.push('<option value="' + i + '">' + placemarks[i].address + '</option>');
								}
								s.push('</select>');

								var place = placemarks[0];
								var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
								marker = new GMarker(point);
								map.addOverlay(marker);
								marker.openInfoWindowHtml('<div style="font-size:13px;">' +
									'<b>你现在所点击的地址:</b><br/>' + s.join('') + '<br/><br/>' +
									'<b>准确度:</b>' + place.AddressDetails.Accuracy + '</div>');
							}

						});
					}
				});
			}
		}
   	//]]>
		</script>
	</head>
	<body onload="setupMap()" onunload="GUnload()">
		<div id="map"></div>
	</body>
</html>